import useInsertNameFormStore from '@renderer/store/Form/InsertNameFormStore'

let positionFn: (tableDataItem: ITableDataItem, insertValue: string) => void

// 新文件名
export default function useInsertNameTransfer(
  callback: () => ITableDataItem[]
) {
  const insertNameTransfer = () => {
    const { insertNamePosition, insertNameAfterCharacter, insertNameContent } =
      useInsertNameFormStore()

    if (insertNameContent) {
      switch (insertNamePosition) {
        case '0':
          positionFn = (tableDataItem, insertValue) => {
            tableDataItem.newest.name = insertValue + tableDataItem.newest.name
          }
          break
        case '1':
          positionFn = (tableDataItem, insertValue) => {
            tableDataItem.newest.name = tableDataItem.newest.name + insertValue
          }
          break
        case '2':
          positionFn = (tableDataItem, insertValue) => {
            const { name } = tableDataItem.newest
            const arr = name.split('')
            arr.splice(insertNameAfterCharacter, 0, insertValue)
            tableDataItem.newest.name = arr.join('')
          }
          break
      }

      callback().forEach((item) => positionFn(item, insertNameContent))
    }
  }

  return {
    insertNameTransfer
  }
}
